home *** CD-ROM | disk | FTP | other *** search
- Path: hubcap.clemson.edu!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Newsgroups: comp.lang.c
- Subject: Re: need psudeo code for binary search
- Date: 2 Mar 96 17:26:06 GMT
- Organization: Clemson University
- Message-ID: <mjs.825787566@hubcap>
- References: <Pine.SOL.3.91.960229154211.27358B-100000@obelix> <4h77mu$qtb@news1.cle.ab.com> <313876FF.2403@ix.netcom.com>
- NNTP-Posting-Host: hubcap.clemson.edu
- X-Newsreader: NN version 6.5.0 #1
-
- Norman Bullen <nbullen@ix.netcom.com> writes:
-
- >Donald-Anthony C. Phillips wrote:
- >>
- >> The binary search algorithm works as follows:
- >> 1) Remember the structure must already be sorted.
- >> 2) It works better as a recursive function
- >It actually works better when written as a loop because it will use much
- >less stack space.
-
- Well, "much less" is relative. The stack space is proportional to the
- log of the size of the list. In the worst case, invocation to a depth
- of 32 calls will be enough to search a list of length 4,294,967,296. A
- depth of 64 will be enough to search a list of length
- 184,467,440,737,009,551,616.
-
- Having said that, if you are doing lots of lookups, a loop is likely
- to be faster. On the other hand, the recursion is a tail recursion,
- so a good compiler might be able to optimize it away.
-
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-